/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: messageparser.h 395 2009-05-07 21:13:25Z jpnurmi $
*/

#ifndef MESSAGEPARSER_H
#define MESSAGEPARSER_H

#include <QMap>
#include <QObject>
#include <QStringList>
#include "alias.h"

class QTextStream;

class MessageParser : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QStringList receivers READ receivers WRITE setReceivers)

public:
    MessageParser(QObject* parent = 0);

    static QStringList commands();

    QStringList receivers() const;
    void setReceivers(const QStringList& receivers);

    void connectSlotsByName(const QObject* receiver);

    enum WordFlag
    {
        AnyWord     = 0x01,
        Receiver    = 0x02,
        Channel     = 0x04,
        AnyReceiver = Receiver | Channel,
        Number      = 0x08,
        Prefixed    = 0x10
    };
    Q_DECLARE_FLAGS(WordFlags, WordFlag)

public slots:
    void parse(const QString& message, const QString& defaultReceiver);

signals:
    void connectTo(const QString& host, quint16 port, const QString& password, const QString& nick);
    void disconnectFrom(const QString& message);
    void quit(const QString& message);

    void sendRaw(const QString& message);

    void cmdHelp(const QString& message);
    void cmdJoin(const QString& channel, const QString& key);
    void cmdPart(const QString& channel, const QString& message);
    void cmdInvite(const QString& nick, const QString& channel);
    void cmdNames(const QString& channel);
    void cmdList(const QString& channel);
    void cmdTopic(const QString& channel, const QString& topic);
    void cmdMode(const QString& target, const QString& mode);
    void setNick(const QString& nick);
    void cmdWhois(const QString& nick);
    void cmdMessage(const QString& receiver, const QString& message);
    void cmdNotice(const QString& receiver, const QString& message);
    void cmdKick(const QString& nick, const QString& channel, const QString& reason);
    void cmdCtcpAction(const QString& receiver, const QString& message);
    void cmdCtcpRequest(const QString& nick, const QString& request);
    void cmdCtcpReply(const QString& nick, const QString& reply);

    void openView(const QString& view);
    void closeView(const QString& view, const QString& reason);
    void clearView(const QString& view);

private:
    void helper(const Alias& alias = Alias());
    QString parseWord(QTextStream& stream, WordFlags flags, const QString& defaultValue = QString()) const;
    QStringList parseWords(QTextStream& stream, WordFlags flag, const QString& defaultValue = QString()) const;
    static void initialize();

    struct MessageParserData
    {
        QStringList receivers;
        static QMap<QString, QString> commands;
    } d;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(MessageParser::WordFlags)

#endif // MESSAGEPARSER_H
